//
//  OldPSViewController.m
//  specialine
//
//  Created by 往返网络 on 17/3/17.
//  Copyright © 2017年 com. All rights reserved.
//

#import "OldPSViewController.h"
#import "PublicMethod.h"

//定义修改密码URL
#define URL_SAVEPS [NSString stringWithFormat:@"%@/Appcustomernew/Personal/changePwddByPwd?type=get",BASE_URL]


@interface OldPSViewController ()<UITextFieldDelegate>
@property (nonatomic,strong)AFHTTPSessionManager* manger;

@end

@implementation OldPSViewController
{

    UITextField * oldtf; //旧密码
    
    UITextField * newtf;  //新密码

    UISwitch * oldswitch;  //旧开关
    
    UISwitch * newswitch;
    
    UIButton * savebtn;
    
    NSString * token;
    
}

- (void)viewWillAppear:(BOOL)animated{


    self.navigationController.navigationBar.hidden = YES;
    
    self.tabBarController.tabBar.hidden = YES;


}

- (void)viewWillDisappear:(BOOL)animated{
    

    self.navigationController.navigationBar.hidden = NO;
    
    self.tabBarController.tabBar.hidden = NO;
    
    
}

- (void)viewDidLoad {

    
    [super viewDidLoad];
    
    [self createUI];
    
    
    // Do any additional setup after loading the view.
}

- (void)createUI{


    self.view.backgroundColor = COLORRGBA(239, 239, 239, 1);
    
    UIView * titleview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_W, 128.0/1334*SCREEN_H)];
    
    titleview.backgroundColor =  COLORRGBA(7, 24, 57, 1);
    
    [self.view addSubview:titleview];
    
    
    float fontsize = 18;
    
    float corensize = 17;
    
    fontsize = SCREEN_H < 667 ? 16 : 20 ;
    
    corensize = SCREEN_H < 667 ? 15 : 19 ;
    
    
    if (SCREEN_H == 667) {
        
        fontsize = 18;
        
        corensize = 17;
        
    }
    
    
    UIButton * leftlable  = [[UIButton alloc]initWithFrame:CGRectMake(0, 20+14.0/1334*SCREEN_H, 80.0/W_BASE*SCREEN_W, 60.0/1334.0*SCREEN_H)];
    
    [leftlable setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    leftlable.titleLabel.font = [UIFont fontWithName:@"iconfont" size:fontsize+3];
    
    [leftlable setTitle:@"\U0000e697" forState:UIControlStateNormal];
    
    [leftlable.titleLabel setTextAlignment:NSTextAlignmentCenter];
    
    [leftlable addTarget:self action:@selector(enterClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [titleview addSubview:leftlable];
    
    UILabel * titlelab = [[UILabel alloc]initWithFrame:CGRectMake(0, CGRectGetMinY(leftlable.frame), SCREEN_W, CGRectGetHeight(leftlable.frame))];
    
    titlelab.textAlignment = NSTextAlignmentCenter;
    
    titlelab.textColor = [UIColor whiteColor];
    
    titlelab.font = [UIFont systemFontOfSize:fontsize - 1];
    
    titlelab.text = @"修改密码";
    
    [titleview addSubview:titlelab];
    
    
    //设置页面主体
    
    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(titleview.frame)+20.0/H_BASE*SCREEN_H, SCREEN_W, 160.0/H_BASE*SCREEN_H)];
    
    view.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:view];
    
    UIView * breakline = [[UIView alloc]initWithFrame:CGRectMake(0, 80.0/H_BASE*SCREEN_H, SCREEN_W, 1)];
    
    breakline.backgroundColor = COLORRGBA(239, 239, 239, 1);
    
    [view addSubview:breakline];
    
    
    oldtf = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, 400.0/W_BASE*SCREEN_W, 80.0/H_BASE*SCREEN_H)];
    
    oldtf.placeholder = @"旧密码";
    
    oldtf.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 30.0/W_BASE*SCREEN_W, 80.0/H_BASE*SCREEN_H)];
    
    oldtf.leftViewMode = UITextFieldViewModeAlways;
    oldtf.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
    
    [view addSubview:oldtf];
    
    oldtf.font = [UIFont systemFontOfSize:fontsize - 4];
    
    
    newtf = [[UITextField alloc]initWithFrame:CGRectMake(0, 80.0/H_BASE*SCREEN_H, 400.0/W_BASE*SCREEN_W, 80.0/H_BASE*SCREEN_H)];
    
    newtf.placeholder = @"新密码";
    
    newtf.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 30.0/W_BASE*SCREEN_W, 80.0/H_BASE*SCREEN_H)];
    
    newtf.leftViewMode = UITextFieldViewModeAlways;
    
    [view addSubview:newtf];
    
    newtf.font = [UIFont systemFontOfSize:fontsize - 4];
    newtf.keyboardType = UIKeyboardTypeNumbersAndPunctuation;

    oldtf.delegate = self;
    
    newtf.delegate = self;
    
    oldswitch = [[UISwitch alloc]initWithFrame:CGRectMake(SCREEN_W - 132.0/W_BASE*SCREEN_W, 9.0/H_BASE*SCREEN_H, 60.0/W_BASE*SCREEN_W, 35.0/H_BASE*SCREEN_H)];
    
    [view addSubview:oldswitch];
    
    oldswitch.tag = 5000;
    
    [oldswitch addTarget:self action:@selector(switchClick) forControlEvents:UIControlEventValueChanged];
    
    oldswitch.onTintColor = COLORRGBA(84, 168, 253, 1);
    
    newswitch = [[UISwitch alloc]initWithFrame:CGRectMake(SCREEN_W - 132.0/W_BASE*SCREEN_W, 89.0/H_BASE*SCREEN_H, 60.0/W_BASE*SCREEN_W, 35.0/H_BASE*SCREEN_H)];
    
    newswitch.tag = 6000;
    
    [newswitch addTarget:self action:@selector(switchClick1) forControlEvents:UIControlEventValueChanged];

    [view addSubview:newswitch];
    
    newswitch.onTintColor = COLORRGBA(84, 168, 253, 1);
    
    //savebtn
    savebtn  = [[UIButton alloc]initWithFrame:CGRectMake(30.0/W_BASE*SCREEN_W, CGRectGetMaxY(view.frame)+60.0/H_BASE*SCREEN_H, 690.0/W_BASE*SCREEN_W, 80.0/H_BASE*SCREEN_H)];
    savebtn.backgroundColor = COLORRGBA(59, 214, 117, 0.5);
    
    savebtn.layer.cornerRadius = 2;
    
    savebtn.clipsToBounds = YES;
    
    [self.view addSubview:savebtn];
    
    [savebtn setTitle:@"保存" forState:UIControlStateNormal];
    
    [savebtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    savebtn.titleLabel.font = [UIFont systemFontOfSize:fontsize - 3];
    
    [savebtn addTarget:self action:@selector(saveNow:) forControlEvents:UIControlEventTouchUpInside];
    
    savebtn.userInteractionEnabled = NO;
    
    oldtf.delegate = self;
    
    newtf.delegate = self;
    
    oldtf.secureTextEntry = YES;
    
    newtf.secureTextEntry = YES;
    
}


- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{

    NSString * textstring = [textField.text stringByReplacingCharactersInRange:range withString:string];
    
    if (textstring.length >= 1) {
        
        savebtn.backgroundColor = [UIColor colorWithRed:47.0/255 green:148.0/255 blue:251.0/255 alpha:1];
        
        savebtn.userInteractionEnabled = YES;
        
    }
    return YES;
    
}

#pragma mark  -- - - 提交密码  请求数据  － － －－ － －
- (void)changePassword{
    
    
    
    NSUserDefaults * userdefalut = [NSUserDefaults standardUserDefaults];
    
    token = [userdefalut objectForKey:USER_TOCKE];
    
    NSString * mobile = [userdefalut objectForKey:USERDEFAULTS_PHONE];
    
    NSString * url = [NSString stringWithFormat:@"%@&token=%@&mobile=%@&newPassword=%@&password=%@",URL_SAVEPS,token,mobile,newtf.text,oldtf.text];
    
    NSLog(@"old ps URL %@",url);
    
    //开始动画
    UIImageView * im = [PublicMethod startAnimation];
    
    [self.manger GET:url parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        
        if (responseObject) {
            
            NSDictionary * dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
            
            NSInteger code = [dict[@"code"]integerValue];
            
            
            if (code == 0) {
                
                
                //请求接口成功后。。。。
                
                [im stopAnimating];
                
                ALERT(@"提示", @"登录密码保存成功", self, @"确定", nil);
                
                // 返回到上级页面。。
                
                [self.navigationController popViewControllerAnimated:YES];
                
                
                }else{
                
                    [im stopAnimating];

                ALERT(@"提示", dict[@"info"], self, @"确定", nil);
                
            }
            
            
        }
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        [im stopAnimating];

        ALERT(@"提示", @"网络请求失败", self, @"确定", nil);
        
    }];
    
    
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{

    [self.view endEditing:YES];
    
    return YES;


}
#pragma mark - - - - - - - -Aciton - - - - -- - - -

- (void)enterClick:(UIButton *)btn{


    [self.navigationController popViewControllerAnimated:YES];


}

- (void)switchClick{

// 旧密码可见

    if (oldswitch.isOn) {
        
        oldtf.secureTextEntry = NO;
        
        
    }else{
    
        oldtf.secureTextEntry = YES;
    
    }

}

- (void)switchClick1{

 //新密码可见
    if (newswitch.isOn) {
        
        newtf.secureTextEntry = NO;
        
        
    }else{
        
        newtf.secureTextEntry = YES;
        
    }



}

#pragma mark  - - - --  保存 - - - -  -- -
- (void)saveNow:(UIButton *)btn{

     //保存
    if (oldtf.text.length < 6) {
        ALERT(@"提示", @"旧密码位数有误", self, @"确定", nil);
        return;
    }else if (newtf.text.length <6){
    
        ALERT(@"提示", @"新密码位数有误", self, @"确定", nil);
        return;
    }else if (oldtf.text.length >16){
    
        ALERT(@"提示", @"旧密码位数有误", self, @"确定", nil);
        return;
    }else if (newtf.text.length >16){
    
        ALERT(@"提示", @"新密码位数有误", self, @"确定", nil);
        return;
    }

    [self changePassword];
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{


    [self.view endEditing:YES];
    

}

- (AFHTTPSessionManager*)manger{
    
    _manger = [AFHTTPSessionManager manager];
    
    _manger.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    _manger.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"application/json", nil];
    
    return _manger;
    
    
    
}

- (void)didReceiveMemoryWarning {
    
    
    [super didReceiveMemoryWarning];
    
    
    // Dispose of any resources that can be recreated.
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
